<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>Active Support 监测程序 — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />

<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />

<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />

<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body class="guide">
  <div id="topNav">
    <div class="wrapper">
      <strong class="more-info-label">更多内容 <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
      <span class="red-button more-info-button">
        更多内容
      </span>
      <ul class="more-info-links s-hidden">
        <li class="more-info"><a href="http://weblog.rubyonrails.org/">博客</a></li>
        <li class="more-info"><a href="http://guides.rubyonrails.org/">指南</a></li>
        <li class="more-info"><a href="http://api.rubyonrails.org/">API</a></li>
        <li class="more-info"><a href="http://stackoverflow.com/questions/tagged/ruby-on-rails">提问</a></li>
        <li class="more-info"><a href="https://github.com/rails/rails">到 GitHub 贡献</a></li>
        <li class="more-info"><a href="https://ruby-china.org/">Ruby China 社区</a></li>
      </ul>
    </div>
  </div>
  <div id="header">
    <div class="wrapper clearfix">
      <h1><a href="index.html" title="返回首页">Rails 指南</a></h1>
      <ul class="nav">
        <li><a class="nav-item" href="index.html">首页</a></li>
        <li class="guides-index guides-index-large">
          <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南索引</a>
          <div id="guides" class="clearfix" style="display: none;">
            <hr />
              <dl class="L">
                <dt>新手入门</dt>
                <dd><a href="getting_started.html">Rails 入门</a></dd>
                <dt>模型</dt>
                <dd><a href="active_record_basics.html">Active Record 基础</a></dd>
                <dd><a href="active_record_migrations.html">Active Record 迁移</a></dd>
                <dd><a href="active_record_validations.html">Active Record 数据验证</a></dd>
                <dd><a href="active_record_callbacks.html">Active Record 回调</a></dd>
                <dd><a href="association_basics.html">Active Record 关联</a></dd>
                <dd><a href="active_record_querying.html">Active Record 查询接口</a></dd>
                <dt>视图</dt>
                <dd><a href="layouts_and_rendering.html">Rails 布局和视图渲染</a></dd>
                <dd><a href="form_helpers.html">Action View 表单辅助方法</a></dd>
                <dt>控制器</dt>
                <dd><a href="action_controller_overview.html">Action Controller 概览</a></dd>
                <dd><a href="routing.html">Rails 路由全解</a></dd>
              </dl>
              <dl class="R">
                <dt>深入探索</dt>
                <dd><a href="active_support_core_extensions.html">Active Support 核心扩展</a></dd>
                <dd><a href="i18n.html">Rails 国际化 API</a></dd>
                <dd><a href="action_mailer_basics.html">Action Mailer 基础</a></dd>
                <dd><a href="active_job_basics.html">Active Job 基础</a></dd>
                <dd><a href="testing.html">Rails 应用测试指南</a></dd>
                <dd><a href="security.html">Ruby on Rails 安全指南</a></dd>
                <dd><a href="debugging_rails_applications.html">调试 Rails 应用</a></dd>
                <dd><a href="configuring.html">配置 Rails 应用</a></dd>
                <dd><a href="command_line.html">Rails 命令行</a></dd>
                <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
                <dd><a href="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</a></dd>
                <dd><a href="autoloading_and_reloading_constants.html">自动加载和重新加载常量</a></dd>
                <dd><a href="caching_with_rails.html">Rails 缓存概览</a></dd>
                <dd><a href="api_app.html">使用 Rails 开发只提供 API 的应用</a></dd>
                <dd><a href="action_cable_overview.html">Action Cable 概览</a></dd>
                <dt>扩展 Rails</dt>
                <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
                <dd><a href="generators.html">创建及定制 Rails 生成器和模板</a></dd>
                <dt>为 Ruby on Rails 做贡献</dt>
                <dd><a href="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</a></dd>
                <dd><a href="api_documentation_guidelines.html">API 文档指导方针</a></dd>
                <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</a></dd>
                <dt>维护方针</dt>
                <dd><a href="maintenance_policy.html">Ruby on Rails 的维护方针</a></dd>
                <dt>发布记</dt>
                <dd><a href="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</a></dd>
                <dd><a href="5_0_release_notes.html">Ruby on Rails 5.0 发布记</a></dd>
                <dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 发布记</a></dd>
                <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 发布记</a></dd>
                <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 发布记</a></dd>
                <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 发布记</a></dd>
                <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 发布记</a></dd>
                <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 发布记</a></dd>
                <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 发布记</a></dd>
                <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 发布记</a></dd>
              </dl>
          </div>
        </li>
        <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">贡献</a></li>
        <li><a class="nav-item" href="credits.html">感谢</a></li>
        <li class="guides-index guides-index-small">
          <select class="guides-index-item nav-item">
            <option value="index.html">指南索引</option>
              <optgroup label="新手入门">
                  <option value="getting_started.html">Rails 入门</option>
              </optgroup>
              <optgroup label="模型">
                  <option value="active_record_basics.html">Active Record 基础</option>
                  <option value="active_record_migrations.html">Active Record 迁移</option>
                  <option value="active_record_validations.html">Active Record 数据验证</option>
                  <option value="active_record_callbacks.html">Active Record 回调</option>
                  <option value="association_basics.html">Active Record 关联</option>
                  <option value="active_record_querying.html">Active Record 查询接口</option>
              </optgroup>
              <optgroup label="视图">
                  <option value="layouts_and_rendering.html">Rails 布局和视图渲染</option>
                  <option value="form_helpers.html">Action View 表单辅助方法</option>
              </optgroup>
              <optgroup label="控制器">
                  <option value="action_controller_overview.html">Action Controller 概览</option>
                  <option value="routing.html">Rails 路由全解</option>
              </optgroup>
              <optgroup label="深入探索">
                  <option value="active_support_core_extensions.html">Active Support 核心扩展</option>
                  <option value="i18n.html">Rails 国际化 API</option>
                  <option value="action_mailer_basics.html">Action Mailer 基础</option>
                  <option value="active_job_basics.html">Active Job 基础</option>
                  <option value="testing.html">Rails 应用测试指南</option>
                  <option value="security.html">Ruby on Rails 安全指南</option>
                  <option value="debugging_rails_applications.html">调试 Rails 应用</option>
                  <option value="configuring.html">配置 Rails 应用</option>
                  <option value="command_line.html">Rails 命令行</option>
                  <option value="asset_pipeline.html">Asset Pipeline</option>
                  <option value="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</option>
                  <option value="autoloading_and_reloading_constants.html">自动加载和重新加载常量</option>
                  <option value="caching_with_rails.html">Rails 缓存概览</option>
                  <option value="api_app.html">使用 Rails 开发只提供 API 的应用</option>
                  <option value="action_cable_overview.html">Action Cable 概览</option>
              </optgroup>
              <optgroup label="扩展 Rails">
                  <option value="rails_on_rack.html">Rails on Rack</option>
                  <option value="generators.html">创建及定制 Rails 生成器和模板</option>
              </optgroup>
              <optgroup label="为 Ruby on Rails 做贡献">
                  <option value="contributing_to_ruby_on_rails.html">为 Ruby on Rails 做贡献</option>
                  <option value="api_documentation_guidelines.html">API 文档指导方针</option>
                  <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导方针</option>
              </optgroup>
              <optgroup label="维护方针">
                  <option value="maintenance_policy.html">Ruby on Rails 的维护方针</option>
              </optgroup>
              <optgroup label="发布记">
                  <option value="upgrading_ruby_on_rails.html">Ruby on Rails 升级指南</option>
                  <option value="5_0_release_notes.html">Ruby on Rails 5.0 发布记</option>
                  <option value="4_2_release_notes.html">Ruby on Rails 4.2 发布记</option>
                  <option value="4_1_release_notes.html">Ruby on Rails 4.1 发布记</option>
                  <option value="4_0_release_notes.html">Ruby on Rails 4.0 发布记</option>
                  <option value="3_2_release_notes.html">Ruby on Rails 3.2 发布记</option>
                  <option value="3_1_release_notes.html">Ruby on Rails 3.1 发布记</option>
                  <option value="3_0_release_notes.html">Ruby on Rails 3.0 发布记</option>
                  <option value="2_3_release_notes.html">Ruby on Rails 2.3 发布记</option>
                  <option value="2_2_release_notes.html">Ruby on Rails 2.2 发布记</option>
              </optgroup>
          </select>
        </li>
      </ul>
    </div>
  </div>
  <hr class="hide" />

  <div id="feature">
    <div class="wrapper">
      <h2>Active Support 监测程序</h2><p>Active Support 是 Rails 核心的一部分，提供 Ruby 语言扩展、实用方法等。其中包括一份监测 API，在应用中可以用它测度 Ruby 代码（如 Rails 应用或框架自身）中的特定操作。不过，这个 API 不限于只能在 Rails 中使用，如果愿意，也可以在其他 Ruby 脚本中使用。</p><p>本文教你如何使用 Active Support 中的监测 API 测度 Rails 和其他 Ruby 代码中的事件。</p><p>读完本文后，您将学到：</p>
<ul>
<li>  使用监测程序能做什么；</li>
<li>  Rails 框架为监测提供的钩子；</li>
<li>  订阅钩子；</li>
<li>  自定义监测点。</li>
</ul>


              <div id="subCol">
          <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />目录</h3>
          <ol class="chapters">
<li><a href="#introduction-to-instrumentation">监测程序简介</a></li>
<li><a href="#rails-framework-hooks">Rails 框架中的钩子</a></li>
<li>
<a href="#action-controller">Action Controller</a>

<ul>
<li><a href="#write-fragment-action-controller">write_fragment.action_controller</a></li>
<li><a href="#read-fragment-action-controller">read_fragment.action_controller</a></li>
<li><a href="#expire-fragment-action-controller">expire_fragment.action_controller</a></li>
<li><a href="#exist-fragment-questionmark-action-controller">exist_fragment?.action_controller</a></li>
<li><a href="#write-page-action-controller">write_page.action_controller</a></li>
<li><a href="#expire-page-action-controller">expire_page.action_controller</a></li>
<li><a href="#start-processing-action-controller">start_processing.action_controller</a></li>
<li><a href="#process-action-action-controller">process_action.action_controller</a></li>
<li><a href="#send-file-action-controller">send_file.action_controller</a></li>
<li><a href="#send-data-action-controller">send_data.action_controller</a></li>
<li><a href="#redirect-to-action-controller">redirect_to.action_controller</a></li>
<li><a href="#halted-callback-action-controller">halted_callback.action_controller</a></li>
</ul>
</li>
<li>
<a href="#action-view">Action View</a>

<ul>
<li><a href="#render-template-action-view">render_template.action_view</a></li>
<li><a href="#render-partial-action-view">render-partial-action-view</a></li>
<li><a href="#render-collection-action-view">render_collection.action_view</a></li>
</ul>
</li>
<li>
<a href="#active-record">Active Record</a>

<ul>
<li><a href="#sql-active-record">sql.active_record</a></li>
<li><a href="#instantiation-active-record">instantiation.active_record</a></li>
</ul>
</li>
<li>
<a href="#action-mailer">Action Mailer</a>

<ul>
<li><a href="#receive-action-mailer">receive.action_mailer</a></li>
<li><a href="#deliver-action-mailer">deliver.action_mailer</a></li>
</ul>
</li>
<li>
<a href="#active-support">Active Support</a>

<ul>
<li><a href="#cache-read-active-support">cache_read.active_support</a></li>
<li><a href="#cache-generate-active-support">cache_generate.active_support</a></li>
<li><a href="#cache-fetch-hit-active-support">cache_fetch_hit.active_support</a></li>
<li><a href="#cache-write-active-support">cache_write.active_support</a></li>
<li><a href="#cache-delete-active-support">cache_delete.active_support</a></li>
<li><a href="#cache-exist-questionmark-active-support">cache_exist?.active_support</a></li>
</ul>
</li>
<li>
<a href="#active-job">Active Job</a>

<ul>
<li><a href="#enqueue-at-active-job">enqueue_at.active_job</a></li>
<li><a href="#enqueue-active-job">enqueue.active_job</a></li>
<li><a href="#perform-start-active-job">perform_start.active_job</a></li>
<li><a href="#perform-active-job">perform.active_job</a></li>
</ul>
</li>
<li>
<a href="#railties">Railties</a>

<ul>
<li><a href="#load-config-initializer-railties">load_config_initializer.railties</a></li>
</ul>
</li>
<li>
<a href="#rails">Rails</a>

<ul>
<li><a href="#deprecation-rails">deprecation.rails</a></li>
</ul>
</li>
<li><a href="#subscribing-to-an-event">订阅事件</a></li>
<li><a href="#creating-custom-events">自定义事件</a></li>
</ol>

        </div>

    </div>
  </div>

  <div id="container">
    <div class="wrapper">
      <div id="mainCol">
        <div class="note"><p>本文原文尚未完工！</p></div><p><a class="anchor" id="introduction-to-instrumentation"></a></p><h3 id="introduction-to-instrumentation">1 监测程序简介</h3><p>Active Support 提供的监测 API 允许开发者提供钩子，供其他开发者订阅。在 Rails 框架中，有很多。通过这个 API，开发者可以选择在应用或其他 Ruby 代码中发生特定事件时接收通知。</p><p>例如，Active Record 中有一个钩子，在每次使用 SQL 查询数据库时调用。开发者可以订阅这个钩子，记录特定操作执行的查询次数。还有一个钩子在控制器的动作执行前后调用，记录动作的执行时间。</p><p>在应用中甚至还可以自己创建事件，然后订阅。</p><p><a class="anchor" id="rails-framework-hooks"></a></p><h3 id="rails-framework-hooks">2 Rails 框架中的钩子</h3><p>Ruby on Rails 框架为很多常见的事件提供了钩子。下面详述。</p><p><a class="anchor" id="action-controller"></a></p><h3 id="action-controller">3 Action Controller</h3><p><a class="anchor" id="write-fragment-action-controller"></a></p><h4 id="write-fragment-action-controller">3.1 write_fragment.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>完整的键</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'posts/1-dashboard-view'
}

</pre>
</div>
<p><a class="anchor" id="read-fragment-action-controller"></a></p><h4 id="read-fragment-action-controller">3.2 read_fragment.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>完整的键</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'posts/1-dashboard-view'
}

</pre>
</div>
<p><a class="anchor" id="expire-fragment-action-controller"></a></p><h4 id="expire-fragment-action-controller">3.3 expire_fragment.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>完整的键</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'posts/1-dashboard-view'
}

</pre>
</div>
<p><a class="anchor" id="exist-fragment-questionmark-action-controller"></a></p><h4 id="exist-fragment-questionmark-action-controller">3.4 exist_fragment?.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>完整的键</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'posts/1-dashboard-view'
}

</pre>
</div>
<p><a class="anchor" id="write-page-action-controller"></a></p><h4 id="write-page-action-controller">3.5 write_page.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:path</code></td>
<td>完整的路径</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  path: '/users/1'
}

</pre>
</div>
<p><a class="anchor" id="expire-page-action-controller"></a></p><h4 id="expire-page-action-controller">3.6 expire_page.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:path</code></td>
<td>完整的路径</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  path: '/users/1'
}

</pre>
</div>
<p><a class="anchor" id="start-processing-action-controller"></a></p><h4 id="start-processing-action-controller">3.7 start_processing.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:controller</code></td>
<td>控制器名</td>
</tr>
<tr>
<td><code>:action</code></td>
<td>动作名</td>
</tr>
<tr>
<td><code>:params</code></td>
<td>请求参数散列，不过滤</td>
</tr>
<tr>
<td><code>:headers</code></td>
<td>请求首部</td>
</tr>
<tr>
<td><code>:format</code></td>
<td>html、js、json、xml 等</td>
</tr>
<tr>
<td><code>:method</code></td>
<td>HTTP 请求方法</td>
</tr>
<tr>
<td><code>:path</code></td>
<td>请求路径</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  controller: "PostsController",
  action: "new",
  params: { "action" =&gt; "new", "controller" =&gt; "posts" },
  headers: #&lt;ActionDispatch::Http::Headers:0x0055a67a519b88&gt;,
  format: :html,
  method: "GET",
  path: "/posts/new"
}

</pre>
</div>
<p><a class="anchor" id="process-action-action-controller"></a></p><h4 id="process-action-action-controller">3.8 process_action.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:controller</code></td>
<td>控制器名</td>
</tr>
<tr>
<td><code>:action</code></td>
<td>动作名</td>
</tr>
<tr>
<td><code>:params</code></td>
<td>请求参数散列，不过滤</td>
</tr>
<tr>
<td><code>:headers</code></td>
<td>请求首部</td>
</tr>
<tr>
<td><code>:format</code></td>
<td>html、js、json、xml 等</td>
</tr>
<tr>
<td><code>:method</code></td>
<td>HTTP 请求方法</td>
</tr>
<tr>
<td><code>:path</code></td>
<td>请求路径</td>
</tr>
<tr>
<td><code>:status</code></td>
<td>HTTP 状态码</td>
</tr>
<tr>
<td><code>:view_runtime</code></td>
<td>花在视图上的时间量（ms）</td>
</tr>
<tr>
<td><code>:db_runtime</code></td>
<td>执行数据库查询的时间量（ms）</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  controller: "PostsController",
  action: "index",
  params: {"action" =&gt; "index", "controller" =&gt; "posts"},
  headers: #&lt;ActionDispatch::Http::Headers:0x0055a67a519b88&gt;,
  format: :html,
  method: "GET",
  path: "/posts",
  status: 200,
  view_runtime: 46.848,
  db_runtime: 0.157
}

</pre>
</div>
<p><a class="anchor" id="send-file-action-controller"></a></p><h4 id="send-file-action-controller">3.9 send_file.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:path</code></td>
<td>文件的完整路径</td>
</tr>
</tbody>
</table>
<div class="info"><p>调用方可以添加额外的键。</p></div><p><a class="anchor" id="send-data-action-controller"></a></p><h4 id="send-data-action-controller">3.10 send_data.action_controller</h4><p><code>ActionController</code> 在载荷（payload）中没有任何特定的信息。所有选项都传到载荷中。</p><p><a class="anchor" id="redirect-to-action-controller"></a></p><h4 id="redirect-to-action-controller">3.11 redirect_to.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:status</code></td>
<td>HTTP 响应码</td>
</tr>
<tr>
<td><code>:location</code></td>
<td>重定向的 URL</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  status: 302,
  location: "http://localhost:3000/posts/new"
}

</pre>
</div>
<p><a class="anchor" id="halted-callback-action-controller"></a></p><h4 id="halted-callback-action-controller">3.12 halted_callback.action_controller</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:filter</code></td>
<td>过滤暂停的动作</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  filter: ":halting_filter"
}

</pre>
</div>
<p><a class="anchor" id="action-view"></a></p><h3 id="action-view">4 Action View</h3><p><a class="anchor" id="render-template-action-view"></a></p><h4 id="render-template-action-view">4.1 render_template.action_view</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:identifier</code></td>
<td>模板的完整路径</td>
</tr>
<tr>
<td><code>:layout</code></td>
<td>使用的布局</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  identifier: "/Users/adam/projects/notifications/app/views/posts/index.html.erb",
  layout: "layouts/application"
}

</pre>
</div>
<p><a class="anchor" id="render-partial-action-view"></a></p><h4 id="render-partial-action-view">4.2 render-partial-action-view</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:identifier</code></td>
<td>模板的完整路径</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  identifier: "/Users/adam/projects/notifications/app/views/posts/_form.html.erb"
}

</pre>
</div>
<p><a class="anchor" id="render-collection-action-view"></a></p><h4 id="render-collection-action-view">4.3 render_collection.action_view</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:identifier</code></td>
<td>模板的完整路径</td>
</tr>
<tr>
<td><code>:count</code></td>
<td>集合的大小</td>
</tr>
<tr>
<td><code>:cache_hits</code></td>
<td>从缓存中获取的局部视图数量</td>
</tr>
</tbody>
</table>
<p>仅当渲染集合时设定了 <code>cached: true</code> 选项，才有 <code>:cache_hits</code> 键。</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  identifier: "/Users/adam/projects/notifications/app/views/posts/_post.html.erb",
  count: 3,
  cache_hits: 0
}

</pre>
</div>
<p><a class="anchor" id="active-record"></a></p><h3 id="active-record">5 Active Record</h3><p><a class="anchor" id="sql-active-record"></a></p><h4 id="sql-active-record">5.1 sql.active_record</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:sql</code></td>
<td>SQL 语句</td>
</tr>
<tr>
<td><code>:name</code></td>
<td>操作的名称</td>
</tr>
<tr>
<td><code>:connection_id</code></td>
<td><code>self.object_id</code></td>
</tr>
<tr>
<td><code>:binds</code></td>
<td>绑定的参数</td>
</tr>
<tr>
<td><code>:cached</code></td>
<td>使用缓存的查询时为 <code>true</code>
</td>
</tr>
</tbody>
</table>
<div class="info"><p>适配器也会添加数据。</p></div><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  sql: "SELECT \"posts\".* FROM \"posts\" ",
  name: "Post Load",
  connection_id: 70307250813140,
  binds: []
}

</pre>
</div>
<p><a class="anchor" id="instantiation-active-record"></a></p><h4 id="instantiation-active-record">5.2 instantiation.active_record</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:record_count</code></td>
<td>实例化记录的数量</td>
</tr>
<tr>
<td><code>:class_name</code></td>
<td>记录所属的类</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  record_count: 1,
  class_name: "User"
}

</pre>
</div>
<p><a class="anchor" id="action-mailer"></a></p><h3 id="action-mailer">6 Action Mailer</h3><p><a class="anchor" id="receive-action-mailer"></a></p><h4 id="receive-action-mailer">6.1 receive.action_mailer</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:mailer</code></td>
<td>邮件程序类的名称</td>
</tr>
<tr>
<td><code>:message_id</code></td>
<td>邮件的 ID，由 Mail gem 生成</td>
</tr>
<tr>
<td><code>:subject</code></td>
<td>邮件的主题</td>
</tr>
<tr>
<td><code>:to</code></td>
<td>邮件的收件地址</td>
</tr>
<tr>
<td><code>:from</code></td>
<td>邮件的发件地址</td>
</tr>
<tr>
<td><code>:bcc</code></td>
<td>邮件的密送地址</td>
</tr>
<tr>
<td><code>:cc</code></td>
<td>邮件的抄送地址</td>
</tr>
<tr>
<td><code>:date</code></td>
<td>发送邮件的日期</td>
</tr>
<tr>
<td><code>:mail</code></td>
<td>邮件的编码形式</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  mailer: "Notification",
  message_id: "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail",
  subject: "Rails Guides",
  to: ["users@rails.com", "ddh@rails.com"],
  from: ["me@rails.com"],
  date: Sat, 10 Mar 2012 14:18:09 +0100,
  mail: "..." # 为了节省空间，省略
}

</pre>
</div>
<p><a class="anchor" id="deliver-action-mailer"></a></p><h4 id="deliver-action-mailer">6.2 deliver.action_mailer</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:mailer</code></td>
<td>邮件程序类的名称</td>
</tr>
<tr>
<td><code>:message_id</code></td>
<td>邮件的 ID，由 Mail gem 生成</td>
</tr>
<tr>
<td><code>:subject</code></td>
<td>邮件的主题</td>
</tr>
<tr>
<td><code>:to</code></td>
<td>邮件的收件地址</td>
</tr>
<tr>
<td><code>:from</code></td>
<td>邮件的发件地址</td>
</tr>
<tr>
<td><code>:bcc</code></td>
<td>邮件的密送地址</td>
</tr>
<tr>
<td><code>:cc</code></td>
<td>邮件的抄送地址</td>
</tr>
<tr>
<td><code>:date</code></td>
<td>发送邮件的日期</td>
</tr>
<tr>
<td><code>:mail</code></td>
<td>邮件的编码形式</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  mailer: "Notification",
  message_id: "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail",
  subject: "Rails Guides",
  to: ["users@rails.com", "ddh@rails.com"],
  from: ["me@rails.com"],
  date: Sat, 10 Mar 2012 14:18:09 +0100,
  mail: "..." # 为了节省空间，省略
}

</pre>
</div>
<p><a class="anchor" id="active-support"></a></p><h3 id="active-support">7 Active Support</h3><p><a class="anchor" id="cache-read-active-support"></a></p><h4 id="cache-read-active-support">7.1 cache_read.active_support</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>存储器中使用的键</td>
</tr>
<tr>
<td><code>:hit</code></td>
<td>是否读取了缓存</td>
</tr>
<tr>
<td><code>:super_operation</code></td>
<td>如果使用 <code>#fetch</code> 读取了，添加 <code>:fetch</code>
</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="cache-generate-active-support"></a></p><h4 id="cache-generate-active-support">7.2 cache_generate.active_support</h4><p>仅当使用块调用 <code>#fetch</code> 时使用这个事件。</p>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>存储器中使用的键</td>
</tr>
</tbody>
</table>
<div class="info"><p>写入存储器时，传给 <code>fetch</code> 的选项会合并到载荷中。</p></div><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'name-of-complicated-computation'
}

</pre>
</div>
<p><a class="anchor" id="cache-fetch-hit-active-support"></a></p><h4 id="cache-fetch-hit-active-support">7.3 cache_fetch_hit.active_support</h4><p>仅当使用块调用 <code>#fetch</code> 时使用这个事件。</p>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>存储器中使用的键</td>
</tr>
</tbody>
</table>
<div class="info"><p>传给 <code>fetch</code> 的选项会合并到载荷中。</p></div><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'name-of-complicated-computation'
}

</pre>
</div>
<p><a class="anchor" id="cache-write-active-support"></a></p><h4 id="cache-write-active-support">7.4 cache_write.active_support</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>存储器中使用的键</td>
</tr>
</tbody>
</table>
<div class="info"><p>缓存存储器可能会添加其他键。</p></div><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'name-of-complicated-computation'
}

</pre>
</div>
<p><a class="anchor" id="cache-delete-active-support"></a></p><h4 id="cache-delete-active-support">7.5 cache_delete.active_support</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>存储器中使用的键</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'name-of-complicated-computation'
}

</pre>
</div>
<p><a class="anchor" id="cache-exist-questionmark-active-support"></a></p><h4 id="cache-exist-questionmark-active-support">7.6 cache_exist?.active_support</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:key</code></td>
<td>存储器中使用的键</td>
</tr>
</tbody>
</table>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
{
  key: 'name-of-complicated-computation'
}

</pre>
</div>
<p><a class="anchor" id="active-job"></a></p><h3 id="active-job">8 Active Job</h3><p><a class="anchor" id="enqueue-at-active-job"></a></p><h4 id="enqueue-at-active-job">8.1 enqueue_at.active_job</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:adapter</code></td>
<td>处理作业的 <code>QueueAdapter</code> 对象</td>
</tr>
<tr>
<td><code>:job</code></td>
<td>作业对象</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="enqueue-active-job"></a></p><h4 id="enqueue-active-job">8.2 enqueue.active_job</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:adapter</code></td>
<td>处理作业的 <code>QueueAdapter</code> 对象</td>
</tr>
<tr>
<td><code>:job</code></td>
<td>作业对象</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="perform-start-active-job"></a></p><h4 id="perform-start-active-job">8.3 perform_start.active_job</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:adapter</code></td>
<td>处理作业的 <code>QueueAdapter</code> 对象</td>
</tr>
<tr>
<td><code>:job</code></td>
<td>作业对象</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="perform-active-job"></a></p><h4 id="perform-active-job">8.4 perform.active_job</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:adapter</code></td>
<td>处理作业的 <code>QueueAdapter</code> 对象</td>
</tr>
<tr>
<td><code>:job</code></td>
<td>作业对象</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="railties"></a></p><h3 id="railties">9 Railties</h3><p><a class="anchor" id="load-config-initializer-railties"></a></p><h4 id="load-config-initializer-railties">9.1 load_config_initializer.railties</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:initializer</code></td>
<td>从 <code>config/initializers</code> 中加载的初始化脚本的路径</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="rails"></a></p><h3 id="rails">10 Rails</h3><p><a class="anchor" id="deprecation-rails"></a></p><h4 id="deprecation-rails">10.1 deprecation.rails</h4>
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:message</code></td>
<td>弃用提醒</td>
</tr>
<tr>
<td><code>:callstack</code></td>
<td>弃用的位置</td>
</tr>
</tbody>
</table>
<p><a class="anchor" id="subscribing-to-an-event"></a></p><h3 id="subscribing-to-an-event">11 订阅事件</h3><p>订阅事件是件简单的事，在 <code>ActiveSupport::Notifications.subscribe</code> 的块中监听通知即可。</p><p>这个块接收下述参数：</p>
<ul>
<li>  事件的名称</li>
<li>  开始时间</li>
<li>  结束时间</li>
<li>  事件的唯一 ID</li>
<li>  载荷（参见前述各节）</li>
</ul>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
  # 自己编写的其他代码
  Rails.logger.info "#{name} Received!"
end

</pre>
</div>
<p>每次都定义这些块参数很麻烦，我们可以使用 <code>ActiveSupport::Notifications::Event</code> 创建块参数，如下：</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
  event = ActiveSupport::Notifications::Event.new *args

  event.name      # =&gt; "process_action.action_controller"
  event.duration  # =&gt; 10 (in milliseconds)
  event.payload   # =&gt; {:extra=&gt;information}

  Rails.logger.info "#{event} Received!"
end

</pre>
</div>
<p>多数时候，我们只关注数据本身。下面是只获取数据的简洁方式：</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
  data = args.extract_options!
  data # { extra: :information }
end

</pre>
</div>
<p>此外，还可以订阅匹配正则表达式的事件。这样可以一次订阅多个事件。下面是订阅 <code>ActionController</code> 中所有事件的方式：</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ActiveSupport::Notifications.subscribe /action_controller/ do |*args|
  # 审查所有 ActionController 事件
end

</pre>
</div>
<p><a class="anchor" id="creating-custom-events"></a></p><h3 id="creating-custom-events">12 自定义事件</h3><p>自己添加事件也很简单，繁重的工作都由 <code>ActiveSupport::Notifications</code> 代劳，我们只需调用 <code>instrument</code>，并传入 <code>name</code>、<code>payload</code> 和一个块。通知在块返回后发送。<code>ActiveSupport</code> 会生成起始时间和唯一的 ID。传给 <code>instrument</code> 调用的所有数据都会放入载荷中。</p><p>下面举个例子：</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ActiveSupport::Notifications.instrument "my.custom.event", this: :data do
  # 自己编写的其他代码
end

</pre>
</div>
<p>然后可以使用下述代码监听这个事件：</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data|
  puts data.inspect # {:this=&gt;:data}
end

</pre>
</div>
<p>自己定义事件时，应该遵守 Rails 的约定。事件名称的格式是 <code>event.library</code>。如果应用发送推文，应该把事件命名为 <code>tweet.twitter</code>。</p>

        <h3>反馈</h3>
        <p>
          我们鼓励您帮助提高本指南的质量。
        </p>
        <p>
          如果看到如何错字或错误，请反馈给我们。
          您可以阅读我们的<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">文档贡献</a>指南。
        </p>
        <p>
          您还可能会发现内容不完整或不是最新版本。
          请添加缺失文档到 master 分支。请先确认 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 是否已经修复。
          关于用语约定，请查看<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南指导</a>。
        </p>
        <p>
          无论什么原因，如果你发现了问题但无法修补它，请<a href="https://github.com/rails/rails/issues">创建 issue</a>。
        </p>
        <p>
          最后，欢迎到 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 邮件列表</a>参与任何有关 Ruby on Rails 文档的讨论。
        </p>
        <h4>中文翻译反馈</h4>
        <p>贡献：<a href="https://github.com/ruby-china/guides">https://github.com/ruby-china/guides</a>。</p>
      </div>
    </div>
  </div>

  <hr class="hide" />
  <div id="footer">
    <div class="wrapper">
      <p>本著作采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/">创作共用 署名-相同方式共享 4.0 国际</a> 授权</p>
<p>“Rails”，“Ruby on Rails”，以及 Rails Logo 为 David Heinemeier Hansson 的商标。版权所有</p>

    </div>
  </div>

  <script type="text/javascript" src="javascripts/jquery.min.js"></script>
  <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
  <script type="text/javascript" src="javascripts/guides.js"></script>
  <script type="text/javascript" src="javascripts/syntaxhighlighter.js"></script>
  <script type="text/javascript">
    syntaxhighlighterConfig = {
      autoLinks: false,
    };
    $(guidesIndex.bind);
  </script>
</body>
</html>
